<template>
  <div id="home">
    
    <div class="header">
      模板列表
      <cool-setting></cool-setting>
    </div>

    <div class="list">
      <el-row :gutter="10">

        <el-col 
          :xl="4" :lg="4" :md="6" :sm="6" :xs="12"
          v-for="item in data"
          :key="item.id"
        >
            <el-card class="card" shadow="never" @click="toCreate(item.id)">
              <div class="remove" @click.stop="remove(item.id)">
                  <el-icon size="18px" color="#fff"><Delete /></el-icon>
              </div>
              <div class="thumb">
                <img
                v-if="!item.url"
                src="../../assets/images/empty.jpg"
                class="image empty-image"/>
                <img
                v-else
                :src="item.url"
                class="image"/>
              </div>
              <div class="name">
                <span>{{ item.name }}</span>
              </div>
            </el-card>
        </el-col>

        <!-- <el-col :xl="4" :lg="4" :md="6" :sm="6" :xs="12">
          <el-card class="card" shodow="never">
            <img
              src="../../assets/images/temp.jpeg"
              class="image"/>
            <div class="name">
              <span>添加模板份添加模板份添加模板份</span>
            </div>
          </el-card>
        </el-col> -->

        <!--S添加模板-->
        <el-col :xl="4" :lg="4" :md="6" :sm="6" :xs="12" @click="showAddAlter">
          <el-card class="card" shadow="never">
            <div class="thumb">
              <img
                src="../../assets/images/add-tpl.jpg"
                class="image add-image"/>
              </div>
          </el-card>
        </el-col>
        <!--E添加模板-->

      </el-row>
    </div>

  </div>
</template>

<script lang="ts">
  import { ElMessage,ElMessageBox } from 'element-plus'
  import CoolSetting from './components/setting/setting.vue'
  import { post } from '@/util/url.js'

  export default {
    data() {
      return {
        // 是否加载中
        loading: false,
        // 模板数据列表
        data: []
      }
    },
    components: {
      CoolSetting
    },
    mounted() {
      this.getData()
    },
    methods: {
      /**
       * 打开项目页面
       * @param id 
       */
      toCreate(id) {
        this.$router.push("/create/" + id)
      },
      /**
       * 删除项目
       */
      remove(id) {
        ElMessageBox.confirm("确认删除该项目吗？删除后不可恢复",{
          confirmButtonText: "删除",
          cancelButtonText: "取消"
        })
          .then(() => {
            const Message = ElMessage({
              message: "删除中",
              duration: 0,
              icon: "delete"
            })
            post("tpl/remove",{
              tpl_id: id
            })
              .then(() => {
                this.getData()
              })
              .catch((msg) => {
                ElMessage({
                  type: "error",
                  message: msg
                })
              })
              .finally(() => {
                Message.close()
              })
          })
      },
      /**
       * 获取数据列表
       */
      getData() {
        this.$get('tpl/get')
          .then((data:any) => {
            this.data = data
          })
      },
      // 添加模板
      addTpl(name:string) :any 
      {
        if(this.loading) return false
        this.loading = true
        this.$post('tpl/add',{
          name,
        })
          .then((res:any) => {
            this.getData()
          })
          .catch((msg:any) => {
            ElMessage({
              showClose:true,
              message: msg,
              type: 'error'
            })
          })
          .finally(() => {
            this.loading = false
          })
      },
      /**
       * 打开添加弹窗
       */
      showAddAlter() {
        ElMessageBox.prompt('模板名称',{
          confirmButtonText: '添加',
          cancelButtonText: '取消',
          confirmButtonClass: "alert-add-button",
          cancelButtonClass: "alert-clean-button",
          inputPlaceholder: "请输入模板名称",
          customClass:"alert-box",
          title: "添加模板",
          inputValidator:function(value:any):any {
              if(value == null || value.trim().length <= 0) {
                return '模板名称不能为空';
              }
              if(value.trim().length>6) {
                return '模板名称长度不能超6个文字';
              }
              return true;
          },
          inputErrorMessage: '长度不超6个字符'
        })
        .then(({value}) => {
          this.addTpl(value)
        })
      }
    }
  }
</script>


<style>
.alert-box input:focus{
  border-color: var(--main-bg-color);
}
.alert-box .is-focus{
  box-shadow: 0 0 0 1px var(--main-bg-color) inset;
}
.alert-add-button,.alert-add-button:hover{
  background-color: var(--main-bg-color);
  border-color: var(--main-bg-color);
}
.alert-clean-button:hover{
  border-color: var(--main-bg-color);
  background-color: var(--second-bg-color);
  color: var(--main-bg-color);
}
</style>

<style lang="scss" scoped>
#home {
   .header{
    position: relative;
    background-color: var(--main-bg-color);
    height: 40px;
    line-height: 40px;
    margin-bottom: 20px;
    text-align: center;
    color: #FFFFFF;
    font-size: 14px;
    font-weight: 500;
   }
   .list{
    position: relative;
    width: 90vw;
    margin: 0 auto;
    padding: 10px 10px 0 10px;
    background-color: #ffffff;
    .card{
      position: relative;
      display: flex;
      flex-direction: column;
      justify-content: center;
      cursor: pointer;
      margin-bottom: 10px;
      background-color: var(--second-bg-color);
      height: 350px;
      .remove{
        display: flex;
        justify-content: center;
        align-items: center;
        position: absolute;
        right: 0;
        top: 0;
        width: 30px;
        height: 30px;
        background-color: var(--main-bg-color-rgba);
       }
      .thumb{
        display: flex;
        align-items: center;
        justify-content: center;
        height: 300px;
        .image{
          flex: 1;
          width: 100%;
          height: 100%;
          object-fit: cover;
          object-position: 0% 0%;
        }
        .empty-image{
          object-position: 50% 50%;
        }
        .add-image{
          object-position: 50% 50%;
        }
      }
      .name{
        padding: 10px 0 0 0;
        font-size: 14px;
        text-overflow: ellipsis;
        overflow: hidden;
        white-space: nowrap;
        color: var(--main-text-color);
      }
    }
   }
}
</style>